<?php


class OccupeManager {
    public function __construct(Mypdo $db){
        $this->db= $db;
    }

    public function add($occu = array()){
        $req = $this->db->prepare('INSERT INTO occupe ( id_loc, id_loca ) VALUES ( :id_loc, :id_loca )');
        $req->bindValue(':id_loc', $occu['id_loc'], PDO::PARAM_INT);
        $req->bindValue(':id_loca', $occu['id_loca'], PDO::PARAM_INT);

        $req->execute();

        $locManager = new LocationManager($this->db);
        $locManager->updateStatut($occu['id_loc'],true);

    }


    public function  delOccuByIdLoca($id_loca){
        $sql = 'DELETE FROM immoloc.occupe WHERE id_loca = :id_loca';
        $req = $this->db->prepare($sql);
        $req->bindValue(':id_loca', $id_loca, PDO::PARAM_INT);

        $req->execute();


        $locManager = new LocationManager($this->db);
        foreach ($this->getIdLocByIdLoca($id_loca) as $value) {
            $locManager->updateStatut($value,false);
        }



    }

    public function getIdLocByIdLoca($id_loca)
    {
        $sql = 'SELECT id_loc FROM occupe WHERE id_loca = :id_loca';
        $req = $this->db->prepare($sql);
        $req->bindValue(':id_loca', $id_loca, PDO::PARAM_INT);

        $req->execute();
        return $req->fetchAll(PDO::FETCH_ASSOC);
    }

    public function delOccuByIdLoc($id_loc)
    {
        $sql = 'DELETE FROM immoloc.occupe WHERE id_loc = :id_loc';
        $req = $this->db->prepare($sql);
        $req->bindValue(':id_loc', $id_loc, PDO::PARAM_INT);

        $req->execute();

        $locManager = new LocationManager($this->db);
        $locManager->updateStatut($id_loc,false);

    }
    public function delOccuByIdLocLoca($id_loc,$id_loca)
    {
        $sql = 'DELETE FROM immoloc.occupe WHERE id_loc = :id_loc AND id_loca = :id_loca';
        $req = $this->db->prepare($sql);
        $req->bindValue(':id_loc', $id_loc, PDO::PARAM_INT);
        $req->bindValue(':id_loca', $id_loca, PDO::PARAM_INT);

        $req->execute();

        $locManager = new LocationManager($this->db);
        $locManager->updateStatut($id_loc,false);

    }
} 